<!--
  * 巡检异常处理表
  *
-->
<template>

  <a-modal
      :title="form.id ? '查看' : '添加'"
      width="1000px"
      :open="visibleFlag"
      @cancel="onClose"
      :maskClosable="false"
      :destroyOnClose="true"
  >
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 5 }" >

          <a-row>
<!--                <a-col :span="12">
                      <a-form-item label="巡检任务ID"  name="taskId">
                        <a-input-number disabled style="width: 100%" v-model:value="form.taskId" placeholder="巡检任务ID" />
                      </a-form-item>
                </a-col>-->
                <a-col :span="12">
                      <a-form-item label="巡检线路"   name="recordId">
                        <a-textarea style="width: 100%" disabled v-model:value="form.lineDetail" placeholder="巡检记录id" />
                      </a-form-item>
                </a-col>
<!--                <a-col :span="12">
                      <a-form-item label="组织机构"  name="deptId">
                        <a-input-number style="width: 100%" disabled v-model:value="form.deptId" placeholder="组织机构" />
                      </a-form-item>
                </a-col>-->
                <a-col :span="12">
                      <a-form-item label="异常描述"  name="description">
                        <a-textarea style="width: 100%" v-model:value="form.description" placeholder="异常描述" />
                      </a-form-item>
                </a-col>
                <a-col :span="12">
                      <a-form-item label="指派人"  name="designator">
                        <a-input-number style="width: 100%" disabled v-model:value="form.designator" placeholder="指派人" />
                      </a-form-item>
                </a-col>
                <a-col :span="12">
                      <a-form-item label="指派时间"  name="assignedTime">
                        <a-date-picker show-time disabled valueFormat="YYYY-MM-DD HH:mm:ss" v-model:value="form.assignedTime" style="width: 100%" placeholder="指派时间" />
                      </a-form-item>
                </a-col>
                <a-col :span="12">
                      <a-form-item label="整改建议"  name="handlingOpinions">
                        <a-textarea style="width: 100%"  v-model:value="form.handlingOpinions" placeholder="整改建议" />
                      </a-form-item>
                </a-col>
                <a-col :span="12">
                  <a-form-item label="处理人"  name="handler">
<!--
                        <a-input-number style="width: 100%" v-model:value="form.Handler" placeholder="处理人" />
                      -->

                  <a-select
                      v-model:value="form.handler"
                      style="width: 100%"
                      placeholder="请选择处理人 "

                  >
                    <a-select-option v-for="item in staffData" :key="item.employeeId" :name="item.actualName" :value="item.employeeId">{{
                        item.actualName
                      }}</a-select-option>
                  </a-select>
                  </a-form-item>
                </a-col>
<!--                <a-col :span="12">
                      <a-form-item label="更新时间"  name="modifyTime">
                        <a-date-picker show-time disabled valueFormat="YYYY-MM-DD HH:mm:ss" v-model:value="form.modifyTime" style="width: 100%" placeholder="更新时间" />
                      </a-form-item>
                </a-col>-->
          </a-row>
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-modal>
</template>
<script setup>
import {reactive, ref, nextTick, onMounted} from 'vue';
  import _ from 'lodash';
  import { message } from 'ant-design-vue';
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import { gInspectionAbnormalApi } from '/@/api/business/xjgl/g-inspection-abnormal-api';
  import { smartSentry } from '/@/lib/smart-sentry';
  import {employeeApi} from "/@/api/system/employee-api.js";

  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['reloadList']);

  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false);

  function show(rowData) {
    Object.assign(form, formDefault);
    if (rowData && !_.isEmpty(rowData)) {
      Object.assign(form, rowData);
    }
    visibleFlag.value = true;
    nextTick(() => {
      formRef.value.clearValidate();
    });
  }
  const staffData = ref([]);
  const xlData = ref([]);
  let queryEmployeeParam = {
    departmentId: undefined,
    pageNum: 1,
    pageSize: 500,
  };
  async function getStaffAll() {
    let res = await employeeApi.queryEmployee(queryEmployeeParam);
    staffData.value = res.data.list;
  }
  function onClose() {
    Object.assign(form, formDefault);
    visibleFlag.value = false;
  }

  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref();

  const formDefault = {
    lineDetail:undefined,
          id: undefined,
              taskId: undefined, //巡检任务ID
              recordId: undefined, //巡检记录id
              deptId: undefined, //组织机构
              description: undefined, //异常描述
              designator: undefined, //指派人
              assignedTime: undefined, //指派时间
              handlingOpinions: undefined, //整改建议
              handler: undefined, //处理人
              modifyTime: undefined, //更新时间
  };

  let form = reactive({ ...formDefault });

  const rules = {
  };

  // 点击确定，验证表单
  async function onSubmit() {
    try {
      await formRef.value.validateFields();
      save();
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!');
    }
  }

  // 新建、编辑API
  async function save() {
    SmartLoading.show();
    try {
      if (form.id) {
        await gInspectionAbnormalApi.update(form);
      } else {
        await gInspectionAbnormalApi.add(form);
      }
      message.success('操作成功');
      emits('reloadList');
      onClose();
    } catch (err) {
      smartSentry.captureError(err);
    } finally {
      SmartLoading.hide();
    }
  }

  defineExpose({
    show,
  });
onMounted(() => {
  console.log("组件挂载完成");
  getStaffAll();

});
</script>
